/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package customGuiControl;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JRootPane;
import presentation.proportional.gui;

/**
 *
 * @author emilio
 */
public class roundButton extends roundedPanel{

    private Boolean selected=false;
    private Container root=null;
    private Boolean isSelectedDrawn=false;
    private String selectedImageString="src/images/contatta.png";
    private ArrayList<Integer> absPosition= null;
    private ImagePanel selectedImagePanel= null;
    private Dimension selectedImageDim=null;

    public roundButton(int radius,Color startColor,
                        Color endColor,Float strokeWidth,Color strokeColor,
                        Float proportinalShadowDim, Float gradientPercent,
                        Color shadowColor, Float shadowOpacity,String id,
                        String selectedImageString)
    {

        super(radius, startColor, endColor, strokeWidth, strokeColor,
              proportinalShadowDim, gradientPercent, shadowColor, shadowOpacity, id);
        if(selectedImageString!=null)
        this.selectedImageString=selectedImageString;
    }

    @Override
    protected void paintComponent(Graphics g) {
        if(!this.selected || this.getRoot()==null)
        {
              super.paintComponent(g);
              if(this.selectedImagePanel!=null)
                    this.removeSelectedImage();
        }
        else
        {
            /*if(this.getRoot()!=null)
            {
                if(!this.isSelectedDrawn || this.checkDirty() ){
                    if(this.selectedImagePanel!=null)
                        this.removeSelectedImage();
                    this.isSelectedDrawn=true;
                    this.dropShadowDesigner.original=null;
                    this.dropShadowDesigner.repaint();
                    int x=this.getAbsoluteOffset(this).get(0);
                    int y=this.getAbsoluteOffset(this).get(1);
                    this.selectedImagePanel= new ImagePanel(this.selectedImageString);
                    this.absPosition= this.getAbsoluteOffset(this);
                    this.selectedImageDim= new Dimension((int) ((this.getWidth())*1.2f),
                                                          (int) (this.getHeight()*1.1f));
                    this.selectedImagePanel.setBounds(this.absPosition.get(0),
                                     this.absPosition.get(1),
                                     this.selectedImageDim.width,
                                     this.selectedImageDim.height);
                    ((gui)this.getRoot()).getAbsolutePane().add(this.selectedImagePanel);
                    // ridisegno solo la parte interessata
                    ((gui)this.getRoot()).getAbsolutePane().repaint(this.absPosition.get(0),
                                                                    this.absPosition.get(1),
                                                                   this.selectedImageDim.width,
                                                                   this.selectedImageDim.height);
                }
            }*/
        }  
    }

    public Boolean getSelected() {
        return selected;
    }

    public void setSelected(Boolean selected) {
        this.selected = selected;
    }
    public void toogleSelected()
    {
        if(this.selected)
            this.selected=false;
        else
            this.selected=true;
    }

     public ArrayList<Integer> getAbsoluteOffset(Container parent){
        ArrayList<Integer> AbsOffset= new ArrayList<Integer>();
        int x=this.getX();
        int y=this.getY();
        while(!parent.getClass().equals(JRootPane.class))
        {
            x+=parent.getX();
            x-=parent.getInsets().left;
            y+=parent.getY();
            y-=(parent.getInsets().top);
            //System.out.println("individuato un parent "+parent.toString()+String.valueOf(parent.getY()));
            parent=parent.getParent();
            if(parent==null)
            {
                System.out.println("");
            }
        }
        AbsOffset.add(x);
        AbsOffset.add(y);
        return AbsOffset;
    }

    public Container getRoot() {
        return root;
    }

    public void setRoot(Container root) {
        this.root = root;
    }
    public void removeSelectedImage()
    {
        ((gui)this.getRoot()).getAbsolutePane().remove(this.selectedImagePanel);
        this.selectedImagePanel=null;
        ((gui)this.getRoot()).getAbsolutePane().repaint(this.absPosition.get(0),
                                                                    this.absPosition.get(1),
                                                                   this.selectedImageDim.width,
                                                                   this.selectedImageDim.height);
        this.absPosition=null;
        this.selectedImageDim=null;
        this.isSelectedDrawn=false;
    }
    
}
